import {
  gql,
  QueryResult,
  useQuery,
  useMutation,
  useLazyQuery,
} from "@apollo/client";
// import { DocumentNode } from "@apollo/react-hooks";
import { AxiosInstance } from "axios";

import { ParamsQuery } from "./visualization";

// import { Mutation } from "~/modules/api-server/types";
import { AggregationQuery } from "~/modules/api-server/types";
import { CalculatedParameterInput } from "~/modules/api-server/types2";
import { useApiService } from "~/modules/api-server/use-api-service";
import { HttpResponse } from "~/modules/shared/utils/http-response";
import { getContent } from "~/plugins/api-server";

// 获取参数列表
export const useAtaParamsList = (params: ParamsQuery) => {
  const document = gql`
    query atasParams{
        atasParams(query: {
            aircraftId : "${params.aircraftId}",
            startTime: "${params.startTime}",
            endTime: "${params.endTime}"
        })
        {
            code
            name
            params{
              name
              quality
              desc
            }
      }
    }
  `;

  const { data } = useQuery<QueryResult<any>>(document);
  return data;
};

// 获取分析任务配置
export const useGetTask = (id: string) => {
  const document = gql`
    query task($id: String!) {
      task(id: $id) {
        id
        flights
        calculated_params {
          id
          name
          desc
          expression
        }
        workspace {
          name
          splitType
          setting {
            profileChartParmas
            chartsHeight
            setBgByStateQuantity
            stateQuantityParams
            backgroundColors
          }
          splitCharts {
            name
            chartType
            upperLimit
            lowerLimit
            XOffset
            YOffset
            IsTop
            data {
              title
              color
              paramType
              expression
            }
          }
        }
      }
    }
  `;
  const query = {
    variables: {
      id,
    },
    client: getContent().explorer_apiServer,
  };
  const { data } = useQuery<QueryResult<any>>(document, query);
  return data;
};

// 创建 分析任务
export const createTaskDocument = gql`
  mutation createTask($input: CreateTaskInput!) {
    createTask(input: $input) {
      name
      tailno
      created_at
      uid
    }
  }
`;

// 更新任务配置
export const updateTaskDocument = gql`
  mutation updateTask($input: UpdateTaskInput!) {
    updateTask(input: $input) {
      name
      tailno
      created_at
      updated_at
      uid
    }
  }
`;

//删除分析任务
export const delTaskDocument = gql`
  mutation deletetask($input: DeleteTaskInput!) {
    deleteTask(input: $input) {
      name
      tailno
      created_at
      updated_at
      uid
    }
  }
`;
// 计算列 单元测试
export const getCustomParamUT = async (params: any): Promise<any> => {
  return await useApiService().post(`/task/UT`, params);
};

// 计算参数;
export const useCustomParamData = (params: any) => {
  const document = gql`
    mutation customParamData(query:{
      taskId: "${params.taskId}",
      flightIds: "${params.flightIds}",
      param: "${params.param}",
      expression:"${params.expression}"
    })
      {
        id
        metricCode
        dataSource
        processStatus
      }
    }
  `;
  // const query = {
  //   variables: {
  //     id,
  //   }
  // };
  const { data, refetch } = useQuery<QueryResult<any>>(document);
  return { data, refetch };
};

//自定义参数 新增/删除/修改

export const getCustomParamDocument = gql`
  mutation Params($input: CalculatedParameterInput!) {
    Params(input: $input) {
      id
      name
      desc
      expression
      taskId
    }
  }
`;
// 工作区参数统计值
export const getChartParamStatistics = (params: AggregationQuery) => {
  const document = gql`
    query paramAggregation($AggregationQuery: AggregationQuery!) {
      paramAggregation(query: $AggregationQuery) {
        processStatus
        result {
          param {
            expr
            type
            values {
              aggregationType
              value
            }
          }
        }
      }
    }
  `;
  const query = {
    variables: {
      AggregationQuery: params,
    },
  };

  const [load] = useLazyQuery<QueryResult<any>>(document, query);
  return { load };
};
